$(document).ready(function(){
	var uname = $("#uname").val();
	var data={};
	data["action"]="getReceiveMessage";
	data["value"] = uname;
	data =  JSON.stringify(data);
data=("msg="+encodeURIComponent(data));
	$.ajax({
		url:"/sview/messageServlet",
		dataType : 'xml',
		type:"post",
		data: data,
		success:function(msg){
//			alert($(msg).find("msg").length);
			$(msg).find("msg").each(function(){
				var line = "<tr id="+$(this).children("id").first().text()+">" 
					+"<td><div><input type='checkbox' /></div></td>"
					+"<td>"+$(this).children("sender").first().text()+"</td>"
					+"<td>"+$(this).children("time").first().text()+"</td>"
					+"<td><a href='javascript:void(0)'>"+$(this).children("title").first().text()+"</a></td>"
					+"</tr>";
				$("tbody").append(line);
				$("tbody").show();
			});
			$("tbody tr td a").click(function(){
				var id = $(this).parent().parent().attr('id');
				var tabid = "#"+id;
				$(msg).find("id").each(function(){
					if($(this).text()==id){
						var content = $(this).parent();
						if($(".tabUl").find(tabid).length>0){
							var tab = $(".tabUl").find(tabid).first();
							var old = $(".highlighted");
							$(old).removeClass("highlighted");
							$(old).addClass("faded");
							$(tab).removeClass("faded");
							$(tab).addClass("highlighted");
							$(".msgList").hide();
							$(".msgContent").children().remove();
							$(".msgContent").append("<br/><p><h3>"+$(this).siblings("title").first().text()+"</h3></p>");
							$(".msgContent").append("<p><b class='title'>From:</b>&nbsp;&nbsp&nbsp;"+$(this).siblings("sender").first().text()+"</p>");
							$(".msgContent").append("<p>Time:"+$(this).siblings("time").first().text()+"</p>");
							$(".msgContent").append("<br/><p>Content:"+$(this).siblings("content").first().text()+"</p>");
							$(".msgContent").append("<input type='hidden' id='mid' name='mid' value="+id+" />");
							var client_name=$(this).siblings("sender").first().text();
							$(".accept").bind("click",accept);
							$(".acceptApply").bind("click",function(){ acceptApply(client_name); });
							$(".reject").bind("click",reject);
							$(".rejectApply").bind("click",rejectApply);
							$(".msgContent").show();
						}else{
							var old = $(".highlighted");
							$(old).removeClass("highlighted");
							$(old).addClass("faded");
							$(".tabUl li:gt(0)").remove();
							$(".tabUl").append("<li class='tabLi highlighted' id='"+id+"'>"+$(tabid).children("td:eq(3)").text()+"</li>");
							$(".msgList").hide();
							$(".msgContent").children().remove();
							$(".msgContent").append("<p><h3>"+$(this).siblings("title").first().text()+"</h3></p>");
							$(".msgContent").append("<p><b class='title'>From:</b>&nbsp;&nbsp&nbsp;"+$(this).siblings("sender").first().text()+"</p>");
							$(".msgContent").append("<p><b class='title'>Time:</b>&nbsp;&nbsp&nbsp;"+$(this).siblings("time").first().text()+"</p>");
							$(".msgContent").append("<br/><p><b class='title'>Content:</b><br/><br/>"+$(this).siblings("content").first().text()+"</p>");
							$(".msgContent").append("<input type='hidden' id='mid' name='mid' value="+id+" />");
							var client_name=$(this).siblings("sender").first().text();
							$(".accept").bind("click",accept);
							$(".acceptApply").bind("click",function(){ acceptApply(client_name); });
							$(".reject").bind("click",reject);
							$(".rejectApply").bind("click",rejectApply);
							$(".msgContent").show();
							}
					}
				});
			});
		}
	});
	$(".tabLi").click(function(){
		clickTab($(this));
	});
});
function clickTab(tab){
	var old = $(".highlighted");
	if($(old).attr("id") != $(tab).attr("id")){
		$(old).removeClass("highlighted");
		$(old).addClass("faded");
		$(tab).removeClass("faded");
		$(tab).addClass("highlighted");
		if($(tab).attr("id")=="0"){
			$(".msgList").show();
			$(".msgContent").hide();
		}else{
			$(".msgList").hide();
			$(".msgContent").show();
		}
	}

	$(".tabLi").click(function(){
		clickTab($(this));
	});
}
function removeMsg(){
	var showText;
	var displayLang = $("#display-lang").val();
	if(displayLang == 'en'){
		showText = "Delete all selected messages?";
	}else if(displayLang == 'zh'){
		showText = "删除所有已选中消息？";
	}
	if(confirm(showText)){
		var lines = $("table input:checked").parent().parent().parent();
		var ids="";
		$(lines).each(function(){
			ids += $(this).attr("id")+",";
		});
		var data={};
		data["action"]="removeMessage";
		data["value"]=ids;
		data["uname"] = $("#uname").val();
		data = JSON.stringify(data);
		data = ("msg="+encodeURIComponent(data));
		$.ajax({
			url:"/sview/messageServlet",
			dataType:"text",
			type:"post",
			data:data,
			success:function(text){
				if(text == "success"){
					$(lines).remove();
				}else{
					var displayLang = $("#display-lang").val();
					if(displayLang == 'en'){
						alert("failed, please try latter.");
					}else if(displayLang == 'zh'){
						alert("操作失败，请稍后再试.");
					}
					
				}
			}
		});
	}
}
function accept(){
	var data={};
	data["action"]="accept";
	data["uname"] = $("#uname").val();
	data["gname"] = $("#gname").text();
	data["mid"] = $("#mid").val();
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url:"/sview/messageServlet",
		dataType:"text",
		type:"post",
		data:data,
		success: function(text){
			var displayLang = $("#display-lang").val();
			if(text == "success"){
				if(displayLang == 'en'){
					alert("Welcome to join "+$("#gname").text()+" group!");
				}else if(displayLang == 'zh'){
					alert("欢迎加入 "+$("#gname").text()+" 编辑组!");
				}
				

				var acc = $(".accept");
				acc.after("accept");
				acc.remove();			
				
				var rej = $(".reject");
				rej.after("reject");
				rej.remove();
				//////
			}else{
				if(displayLang == 'en'){
					alert("failed, please try latter.");
				}else if(displayLang == 'zh'){
					alert("操作失败，请稍后再试.");
				}
			}
		}
	});
}
function acceptApply(client_name){
	var data={};
	data["action"]="acceptApply";
	data["uname"] = client_name;
	data["adminname"] = $("#uname").val();
	data["gname"] = $("#gname").text();
	data["mid"] = $("#mid").val();
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url:"/sview/messageServlet",
		dataType:"text",
		type:"post",
		data:data,
		success: function(text){
			var displayLang = $("#display-lang").val();
			if(text == "success"){				
				var acc = $(".acceptApply");
				acc.after("accept");
				acc.remove();			
				
				var rej = $(".rejectApply");
				rej.after("reject");
				rej.remove();
				//////
			}else{
				if(displayLang == 'en'){
					alert("failed, please try latter.");
				}else if(displayLang == 'zh'){
					alert("操作失败，请稍后再试.");
				}
			}
		}
	});
}
function reject(){
	var showText;
	var displayLang = $("#display-lang").val();
	if(displayLang == 'en'){
		showText = "This action cannot be undone. Are you sure to continue?";
	}else if(displayLang == 'zh'){
		showText = "此拒绝操作不可恢复，是否继续？";
	}
	if(confirm(showText)){
		var data={};
		data["action"] = "reject";
		data["uname"] = $("#uname").val();
		data["gname"] = $("#gname").text();
		data["mid"] = $("#mid").val();
		data = JSON.stringify(data);
		data = ("msg="+encodeURIComponent(data));
		$.ajax({
			url:"/sview/messageServlet",
			dataType:"text",
			type:"post",
			data:data,
			success: function(text){
				if(text == "success"){
					var acc = $(".accept");
					acc.after("accept");
					acc.remove();	
					var rej = $(".reject");
					rej.after("reject");
					rej.remove();				
				}else{
					var displayLang = $("#display-lang").val();
					if(displayLang == 'en'){
						alert("failed, please try latter.");
					}else if(displayLang == 'zh'){
						alert("操作失败，请稍后再试.");
					}
				}
			}
		});
	}
}
function rejectApply(){
	var showText="";
	var displayLang = $("#display-lang").val();
	if(displayLang == 'en'){
		showText = "This action cannot be undone. Are you sure to continue?";
	}else if(displayLang == 'zh'){
		showText = "此拒绝操作不可恢复，是否继续？";
	}
	if(confirm(showText)){
		var data={};
		data["action"] = "rejectApply";
		data["uname"] = $("#uname").val();
		data["gname"] = $("#gname").text();
		data["mid"] = $("#mid").val();
		data = JSON.stringify(data);
		data = ("msg="+encodeURIComponent(data));
		$.ajax({
			url:"/sview/messageServlet",
			dataType:"text",
			type:"post",
			data:data,
			success: function(text){
				if(text == "success"){
					var acc = $(".acceptApply");
					acc.after("accept");
					acc.remove();	
					var rej = $(".rejectApply");
					rej.after("reject");
					rej.remove();				
				}else{
					var displayLang = $("#display-lang").val();
					if(displayLang == 'en'){
						alert("failed, please try latter.");
					}else if(displayLang == 'zh'){
						alert("操作失败，请稍后再试.");
					}
				}
			}
		});
	}
}